package rpg002;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

/**
 *
 * @author Smai
 *
 * 
 */
public class ActionListener implements KeyListener, MouseListener, MouseMotionListener {

  private RightClickMenu rightClickMenu;
  private MapEditor mapEditor;
  private Map map;
  public boolean buttonK;
  public boolean arrowUp;
  public boolean arrowDown;
  public boolean arrowLeft;
  public boolean arrowRight;
  public int mouseX;
  public int mouseY;
  public boolean rmbClick;
  public boolean lmbClick;

  public void setActionListener(Map map, MapEditor mapEditor, RightClickMenu rightClickMenu) {
    this.map = map;
    this.mapEditor = mapEditor;
    this.rightClickMenu = rightClickMenu;
  }

  @Override
  public void keyTyped(KeyEvent e) {
  }

  @Override
  public void keyPressed(KeyEvent e) {
    switch (e.getKeyCode()) {

      //Kartan tallennus nappula
      case KeyEvent.VK_O:
        mapEditor.saveMap();
        break;

      case KeyEvent.VK_L:
        mapEditor.nextTile();
        break;

      case KeyEvent.VK_J:
        mapEditor.prevTile();
        break;
      case KeyEvent.VK_UP:
        arrowUp = true;
        break;

      case KeyEvent.VK_DOWN:
        arrowDown = true;
        break;

      case KeyEvent.VK_LEFT:
        arrowLeft = true;
        break;

      case KeyEvent.VK_RIGHT:
        arrowRight = true;
        break;

      case KeyEvent.VK_K:
        if (buttonK) {
          System.out.println("Map editor OFF");
          buttonK = false;
        } else {
          System.out.println("Map editor ON");
          buttonK = true;
        }
        break;

      default:
        break;
    }
  }

  @Override
  public void keyReleased(KeyEvent e) {
    switch (e.getKeyCode()) {
      case KeyEvent.VK_UP:
        arrowUp = false;
        break;

      case KeyEvent.VK_DOWN:
        arrowDown = false;
        break;

      case KeyEvent.VK_LEFT:
        arrowLeft = false;
        break;

      case KeyEvent.VK_RIGHT:
        arrowRight = false;
        break;

      default:
        break;
    }
  }

  public void mouseClicked(MouseEvent e) {
    if (e.getButton() == MouseEvent.BUTTON3) {
      rmbClick = true;
      rightClickMenu.setMenuCoords(e.getX(), e.getY());
    } else if (e.getButton() == MouseEvent.BUTTON1) {
      //lmbClick = true;
      if (rmbClick) {
        rightClickMenu.checkMouseInfo(e.getX(), e.getY());
      } else if (buttonK){
        map.checkMouseInfo(e.getX(), e.getY());
      }
      rmbClick = false;
    }
  }

  public void mousePressed(MouseEvent e) {
  }

  public void mouseReleased(MouseEvent e) {
  }

  public void mouseEntered(MouseEvent e) {
  }

  public void mouseExited(MouseEvent e) {
  }

  public void mouseDragged(MouseEvent e) {
  }

  public void mouseMoved(MouseEvent e) {
    mouseX = e.getX();
    mouseY = e.getY();
  }
}
